using UnityEngine;

public class PreloadSector : MonoBehaviour
{
	public TypeOfSector typeOfSector;

	[Space(10f)]
	public bool isEnabled = true;

	[Space(10f)]
	public int radiusSector = 100;

	public int radiusForLoad = 10;

	public int radiusForUnload = 20;

	[Space(10f)]
	public float unloadTime = 5f;

	[Space(10f)]
	public bool isNeedUnloadSector;

	public bool isForceLoadSector;

	public bool disableForWeakDevices;

	[Space(10f)]
	public EnemyType[] possibleEnemyTypes;

	[Space(10f)]
	public statusSector curStatus;

	public Vector2 centerPoint = Vector3.zero;

	public GameObject objSector;

	public SectorCreate scriptSector;

	private GameObject objPlayer;

	private static int _sectorsCnt;

	private float distance;

	private bool active;

	private void Awake()
	{
	}

	private void Start()
	{
		ManagerPreloadingSectors.thisScript.dictionaryPreloadSectors[(int)typeOfSector].Add(this);
		SectorCreate componentInChildren = GetComponentInChildren<SectorCreate>(true);
		if (componentInChildren == null)
		{
			base.enabled = false;
			BCWDebug.LogError("sectorCreate is null, epta!");
			return;
		}
		objSector = componentInChildren.gameObject;
		scriptSector = componentInChildren;
		scriptSector.Init();
		_sectorsCnt++;
		Camera component = GameController.instance.cameraGame.GetComponent<Camera>();
		if (Device.indexWeakDevice == 2)
		{
			settings.lodSectorRadiusKoef = 0.7f;
		}
		else
		{
			settings.lodSectorRadiusKoef = 0.99f;
		}
		radiusForLoad = Mathf.RoundToInt((float)radiusForLoad * settings.lodSectorRadiusKoef);
		radiusForUnload = Mathf.RoundToInt((float)radiusForUnload * settings.lodSectorRadiusKoef);
	}

	public bool UpdateState(Vector2 posPlayer)
	{
		if (base.enabled)
		{
			return CheckSectorLoadRadius(posPlayer);
		}
		return false;
	}

	private bool CheckSectorLoadRadius(Vector2 posPlayer)
	{
		if (!Device.isWeakDevice || !disableForWeakDevices)
		{
			distance = Vector2.Distance(posPlayer, centerPoint);
			if (distance < (float)(radiusForLoad + radiusSector))
			{
				CancelInvoke("Wait");
				if (!active)
				{
					BCWDebug.Log(base.gameObject.name, "Preloads");
					scriptSector.gameObject.SetActive(true);
					active = true;
					scriptSector.LoadRes();
				}
				return true;
			}
			if (active && distance > (float)(radiusForUnload + radiusSector))
			{
				Invoke("Wait", unloadTime);
				return false;
			}
		}
		return false;
	}

	public void Wait()
	{
		if (objSector.activeSelf)
		{
			scriptSector.ClearMeshes();
			active = false;
			scriptSector.gameObject.SetActive(false);
		}
	}

	public PreloadSector Copy(PreloadSector inSector)
	{
		radiusForLoad = inSector.radiusForLoad;
		radiusForUnload = inSector.radiusForUnload;
		centerPoint = inSector.centerPoint;
		radiusSector = inSector.radiusSector;
		unloadTime = inSector.unloadTime;
		isEnabled = inSector.isEnabled;
		curStatus = inSector.curStatus;
		isNeedUnloadSector = inSector.isNeedUnloadSector;
		isForceLoadSector = inSector.isForceLoadSector;
		return this;
	}
}
